<?php
/**
 * @version 1.6
 * @package RealEstateManagerTop10
 * @copyright 2009 OrdaSoft
 * @author 2009 Sergey Drughinin-OrdaSoft(Sergey.dru@gmail.com)
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @description RealEstateTop10 for RealEstateManager Component
*/
/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

global $mosConfig_live_site;

$doc =& JFactory::getDocument(); 
$doc->addStyleSheet($mosConfig_live_site.'/administrator/components/com_realestatemanager/includes/realestatemanager.css');
$ItemId_tmp_from_params=$params->get('ItemId');
$moduleclass_sfx = $params->get('moduleclass_sfx', '');

if( !function_exists( 'sefreltoabs')) {
  function sefRelToAbs( $value ) {
    //Need check!!!

    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);
    if(substr(strtolower($url),0,9) != "index.php") return $url;
    $uri    = JURI::getInstance();
    $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}
$database = &JFactory::getDBO();

if ( !function_exists('getWhereUsergroupsString')) {
  function getWhereUsergroupsString( $table_alias ) { 
    global $my;
     
    if ( isset($my->id) AND $my->id != 0 ) { 
     
      $usergroups_sh = getGroupsByUser ($my->id,'');       
  
      //$usergroups_sh = '-2'.$usergroups_sh;   
      
    } else {
       $usergroups_sh = array ();
    }
    $usergroups_sh[] = -2; 
      
          
    $s = '';
    for ($i=0; $i<count($usergroups_sh); $i++) {  
      $g = $usergroups_sh[$i];
      $s .= " $table_alias.params LIKE '%,{$g}' or $table_alias.params = '{$g}' or $table_alias.params LIKE '{$g},%' or $table_alias.params LIKE '%,{$g},%' ";
      if ( ($i+1)<count($usergroups_sh) )
        $s .= ' or ';
    } 
    return $s;
  } 
}
$s = getWhereUsergroupsString("c");

$selectstring = "SELECT h.htitle, h.id, h.hits, hc.idcat
		\nFROM #__rem_houses AS h
		\nLEFT JOIN #__rem_categories AS hc ON hc.iditem=h.id
		\nLEFT JOIN #__rem_main_categories AS c ON c.id=hc.idcat
		\nWHERE ($s)
		\nORDER BY hits DESC LIMIT 0,10;";
$database->setQuery($selectstring);
$rows = $database->loadObjectList();

$selectstring = "SELECT id  FROM #__menu WHERE menutype like '%menu%' AND link LIKE '%option=com_realestatemanager%' AND params LIKE '%back_button%";
$database->setQuery($selectstring);
$ItemId_tmp_rom_db = $database->loadResult();
$ItemId_tmp_from_db = $database->loadResult();  
if($ItemId_tmp_from_params==""){
  $ItemId_tmp=$ItemId_tmp_from_db;
}
else{
  $ItemId_tmp=$ItemId_tmp_from_params;
}



//todo load correct itemid
?>
<div class="realestatemanager_<?php if($moduleclass_sfx!='') echo $moduleclass_sfx ; ?>">
<table cellpadding="0" cellspacing="0" class="basictable" width="100%">
   <tr>
      <td>
         <table cellpadding="0" cellspacing="0" class="basictable" width="100%">
	<?php foreach ($rows as $row) { 
	 $link1 ="index.php?option=com_realestatemanager&amp;task=view&amp;id=".$row->id."&amp;catid=".$row->idcat."&amp;Itemid=".$ItemId_tmp;
	?>
		<tr>
                     <td>
                     <a href="<?php echo sefRelToAbs($link1); ?>"><?php echo $row->htitle; ?></a>
                     </td>
		   <td align="right">
                     <font class='small'>(<?php echo $row->hits; ?>)</font>
		   </td>
                  </tr>
           <?php } ?>
           </table>
      </td>
   </tr>
</table>
<div style="text-align: center;"><a href="http://ordasoft.com" style="font-size: 10px;">Powered by OrdaSoft!</a></div>
</div>
